package cn.tedu.medicalmanagementsystem.user.controller;

import cn.tedu.medicalmanagementsystem.base.response.JsonResult;
import cn.tedu.medicalmanagementsystem.base.response.StatusCode;
import cn.tedu.medicalmanagementsystem.user.pojo.dto.ReviewAddParam;
import cn.tedu.medicalmanagementsystem.escort.pojo.dto.ReviewReplyParam;
import cn.tedu.medicalmanagementsystem.user.pojo.vo.PageData;
import cn.tedu.medicalmanagementsystem.user.pojo.vo.ReviewVO;
import cn.tedu.medicalmanagementsystem.user.pojo.vo.UserEscortVO;
import cn.tedu.medicalmanagementsystem.user.pojo.vo.UserVO;
import cn.tedu.medicalmanagementsystem.user.service.ReviewService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/user/reviews")
@Api(tags = "用户评价管理")
public class ReviewController {
    @Autowired
    private ReviewService reviewService;

    @PostMapping("/add")
    @ApiOperation("用户添加评价")
    public JsonResult add(@RequestBody @Validated ReviewAddParam param, HttpSession session) {
        Long userId = getCurrentUserId(session);
        if (userId == null) {
            return JsonResult.fail(StatusCode.NOT_LOGIN);
        }
        return reviewService.addReview(param, userId);
    }

    @GetMapping("/escort/{escortId}")
    @ApiOperation("查看陪诊员评价")
    public JsonResult<List<ReviewVO>> listByEscort(@PathVariable Long escortId) {
        return reviewService.getReviewsByEscortId(escortId);
    }

    @PostMapping("/reply")
    @ApiOperation("陪诊员回复评价")
    public JsonResult reply(@RequestBody @Validated ReviewReplyParam param, HttpSession session) {
        Long escortId = getCurrentEscortId(session);
        if (escortId == null) {
            return JsonResult.fail(StatusCode.NOT_LOGIN);
        }
        return reviewService.replyReview(param, escortId);
    }

    @GetMapping("/user/{userId}")
    @ApiOperation("查看用户评价")
    public JsonResult<List<ReviewVO>> listByUser(@PathVariable Long userId) {
        return reviewService.getReviewsByUserId(userId);
    }

    @GetMapping("/page")
    @ApiOperation("分页查询评价列表")
    public JsonResult<PageData<ReviewVO>> listReviews(
            @RequestParam(required = false) Long userId,
            @RequestParam(required = false) Long escortId,
            @RequestParam(required = false) Integer minRating,
            @RequestParam(required = false) Integer maxRating,
            @RequestParam(required = false) Boolean replied,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            HttpSession session) { // 添加session参数

        // 如果传入了userId参数，优先使用参数值
        // 否则尝试从session中获取当前用户ID
        Long currentUserId = (userId != null) ? userId : getCurrentUserId(session);
        return reviewService.listReviews(currentUserId, escortId, minRating, maxRating, replied, page, size);
    }

    private Long getCurrentUserId(HttpSession session) {
        Object userObj = session.getAttribute("user");
        if (userObj == null) {
            log.warn("Session中没有找到用户信息, session id: {}", session.getId());
            return null;
        }
        UserVO user = (UserVO) userObj;
        return user.getId();
    }

    private Long getCurrentEscortId(HttpSession session) {
        Object escortObj = session.getAttribute("escort");
        if (escortObj == null) {
            log.warn("Session中没有找到陪诊员信息, session id: {}", session.getId());
            return null;
        }
        UserEscortVO escort = (UserEscortVO) escortObj;
        return escort.getId();
    }
}